import { RouteRecordRaw } from 'vue-router'
import MenuModel from '@/model/MenuModel'
import { ElMessage } from 'element-plus'

export function initMenu(routes: RouteRecordRaw[]): Array<MenuModel> {
  if (!routes) {
    return []
  }
  return routes.map((item) => {
    return new MenuModel({
      label: item.meta ? (item.meta.title as string) : '',
      icon: item.meta ? (item.meta.icon as string) : '',
      index: item.path,
      children: item.children ? initMenu(item.children) : []
    })
  })
}

export function copyText(text: string) {
  //创建一个input框
  const input = document.createElement('input')
  //将指定的DOM节点添加到body的末尾
  document.body.appendChild(input)
  //设置input框的value值为直播地址
  input.setAttribute('value', text)
  //选取文本域中的内容
  input.select()
  //copy的意思是拷贝当前选中内容到剪贴板
  //document.execCommand（）方法操纵可编辑内容区域的元素
  //返回值为一个Boolean，如果是 false 则表示操作不被支持或未被启用
  if (document.execCommand('copy')) {
    document.execCommand('copy')
  }
  //删除这个节点
  document.body.removeChild(input)
  ElMessage.success('成功复制到粘贴板')
}

export function hexToRgb(hex: string) {
  // 去除#号并分割成RGB三部分
  let rgb = hex.replace('#', '').match(/[0-9a-fA-F]{2}/g)
  if (!rgb || rgb.length < 3) {
    return []
  }

  return [parseInt(rgb[0], 16), parseInt(rgb[1], 16), parseInt(rgb[2], 16)]
}
export function rgbToHex(r: number, g: number, b: number) {
  r = Math.floor(r) // 取整数部分
  g = Math.floor(g)
  b = Math.floor(b)

  var hexR = r < 0 || isNaN(r) ? '00' : r > 255 ? 'FF' : ('0' + r.toString(16)).slice(-2)
  var hexG = g < 0 || isNaN(g) ? '00' : g > 255 ? 'FF' : ('0' + g.toString(16)).slice(-2)
  var hexB = b < 0 || isNaN(b) ? '00' : b > 255 ? 'FF' : ('0' + b.toString(16)).slice(-2)

  return '#' + hexR + hexG + hexB
}
export function rgbToHsv(r: number, g: number, b: number) {
  r /= 255
  g /= 255
  b /= 255

  let max = Math.max(r, g, b)
  let min = Math.min(r, g, b)
  let h = 0,
    s,
    v = max

  let d = max - min
  s = max === 0 ? 0 : d / max

  if (max == min) {
    h = 0
  } else {
    switch (max) {
      case r:
        h = (g - b) / d + (g < b ? 6 : 0)
        break
      case g:
        h = (b - r) / d + 2
        break
      case b:
        h = (r - g) / d + 4
        break
    }
    h /= 6
  }

  return [h * 360, s, v]
}
export function hsvToRgb(h: number, s: number, v: number) {
  let i, f, p1, p2, p3
  let r = 0,
    g = 0,
    b = 0
  if (s < 0) s = 0
  if (s > 1) s = 1
  if (v < 0) v = 0
  if (v > 1) v = 1
  h %= 360
  if (h < 0) h += 360
  h /= 60
  i = Math.floor(h)
  f = h - i
  p1 = v * (1 - s)
  p2 = v * (1 - s * f)
  p3 = v * (1 - s * (1 - f))
  switch (i) {
    case 0:
      r = v
      g = p3
      b = p1
      break
    case 1:
      r = p2
      g = v
      b = p1
      break
    case 2:
      r = p1
      g = v
      b = p3
      break
    case 3:
      r = p1
      g = p2
      b = v
      break
    case 4:
      r = p3
      g = p1
      b = v
      break
    case 5:
      r = v
      g = p1
      b = p2
      break
  }
  return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)]
}
